//
//  PhoneAddressController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhoneAddressController.h"
#import "NetHander.h"
#import "DataBaseHandler.h"
@interface PhoneAddressController ()
@property (nonatomic, strong) UITextField *phoneNumber;
@property (nonatomic, strong) NSString *contentStr;
@property (nonatomic, strong) UILabel *province;
@property (nonatomic, strong) DataBaseHandler *dataBase;
@end

@implementation PhoneAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataBase = [DataBaseHandler shareInstance];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.title = @"手机归属地查询";
    // 创建输入框
    self.phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, WIDTH - 20, 40)];
    self.phoneNumber.placeholder = @"请输入正确的手机号";
    self.phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.phoneNumber];
    // 创建搜索按钮
    UIButton *search = [UIButton buttonWithType:UIButtonTypeSystem];
    search.frame = CGRectMake(10, 70, WIDTH - 20, 40);
    [search setTitle:@"查询" forState:UIControlStateNormal];
    [self.view addSubview:search];
    [search addTarget:self action:@selector(targetSearch:) forControlEvents:UIControlEventTouchUpInside];
    // 创建label
    self.province = [[UILabel alloc]initWithFrame:CGRectMake(20, 130, WIDTH - 40, 120)];

//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cancle.png"] style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
    if (self.number != 0) {
        self.phoneNumber.text = self.number;
        [self searchSelect];
    }
}
- (void)targetCancle:(UIButton *)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)targetSearch:(UIButton *)search
{
    [self.phoneNumber resignFirstResponder];
    if (self.phoneNumber.text.length != 0) {
        [self.dataBase openDB];
        [self.dataBase createPhone];
        [self.dataBase insertPhone:self.phoneNumber.text];
    }
    [self searchSelect];
}
- (void)searchSelect
{
    NSString *path = [NSString stringWithFormat:@"http://apix.sinaapp.com/mobilephone/?appkey=liming5712ou&number=%@", self.phoneNumber.text];
    [NetHander getDataWithUrl:path completion:^(NSData *data) {
        self.contentStr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"1");
        [self creatLabel];
        NSLog(@"2");
        //        NSLog(@"%@", [self.contentStr objectForKey:@"carrier"]);
    }];
}
- (void)creatLabel{
    // 设置省份
    self.province.numberOfLines = 0;
    self.province.text = self.contentStr;
    [self.view addSubview:self.province];
    
    NSLog(@"==%@", self.contentStr);
//    // 设置catname
//    UILabel *catName = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, WIDTH - 40, 40)];
//    catName.text = [self.contentDic objectForKey:@"catName"];
//    [self.view addSubview:catName];
//    // 设置telString
//    UILabel *telString = [[UILabel alloc] initWithFrame:CGRectMake(20, 230, WIDTH - 40, 40)];
//    telString.text = [self.contentDic objectForKey:@"telString"];
//    [self.view addSubview:telString];
//    // 设置carrier
//    UILabel *carrier = [[UILabel alloc] initWithFrame:CGRectMake(20, 280, WIDTH - 40, 40)];
//    carrier.text = [self.contentDic objectForKey:@"carrier"];
//    [self.view addSubview:carrier];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.phoneNumber resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
